import {
	IAppMenuAbility,
	IIndexViewAbility,
	IIndexViewController,
	IIndexViewControllerParams,
	IIndexViewStore,
	IndexViewActionType,
	IParam,
	IProjectSetting,
	IViewActionResult,
} from '@/core/interface';
import { ViewController } from './view-controller';

/**
 * 首页视图控制器
 *
 * @export
 * @class IndexViewController
 * @extends {ViewController}
 */
export class IndexViewController
	extends ViewController<
		IndexViewActionType,
		IIndexViewStore,
		IIndexViewAbility
	>
	implements IIndexViewController<IIndexViewStore, IIndexViewAbility>
{
	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {IIndexViewControllerParams<IndexViewActionType, IIndexViewAbility>} params
	 * @memberof IndexViewController
	 */
	protected processViewInit(
		params: IIndexViewControllerParams<IndexViewActionType, IIndexViewAbility>
	) {
		super.processViewInit(params);
		const projectSetting: IProjectSetting = App.getProjectSetting();
		Object.assign(this.store, {
			showLang: projectSetting.headerSetting.showLang,
			showOrgSelect: projectSetting.headerSetting.showOrgSelect,
			showThemeSetting: projectSetting.headerSetting.showThemeSetting,
			showNotice: projectSetting.headerSetting.showNotice,
			showLogo: projectSetting.showLogo,
			showTabPageExp: projectSetting.multiTabsSetting.show,
			showBreadCrumb: projectSetting.showBreadCrumb,
			showUserInfo: projectSetting.headerSetting.showUserInfo,
			showFullScreen: projectSetting.headerSetting.showFullScreen,
			showLockScreen: projectSetting.headerSetting.showLockScreen,
			mainMenuPosition: projectSetting.menuSetting.mainMenuPosition,
		});
	}

	/**
	 * @description 获取菜单能力
	 * @protected
	 * @return {*}  {(IAppMenuAbility | undefined)}
	 * @memberof IndexViewController
	 */
	protected getMainCtrlAbility(): IAppMenuAbility | undefined {
		const appMenu = this.model.ctrls.find(
			(ctrl: IParam) => ctrl.controlType === 'APPMENU'
		);
		if (appMenu) {
			return this.getSubAbility<IAppMenuAbility>(appMenu.name);
		}
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [opts={}]
	 * @return {*}
	 * @memberof IndexViewController
	 */
	public async viewMounted(opts: IParam = {}) {
		super.viewMounted(opts);
		App.getGlobalNotificationHelper().setAbility('app', this.getAbility());
		App.closeAppLoadding();
		if (this.model.useDefaultLayout) {
			await this.load();
		} else {
			await this.initLayout();
			await this.load();
		}
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [args]
	 * @return {*}  {Promise<IViewActionResult>}
	 * @memberof IndexViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		const appMenu = this.getMainCtrlAbility();
		if (appMenu) {
			return appMenu.load();
		} else {
			return Promise.reject({
				ok: false,
				data: null,
				rowData: { status: 500 },
			});
		}
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [args]
	 * @return {*}  {Promise<IViewActionResult>}
	 * @memberof IndexViewController
	 */
	public async refresh(args?: IParam): Promise<boolean> {
		const appMenu = this.getMainCtrlAbility();
		if (appMenu) {
			return await appMenu.refresh(args);
		} else {
			return Promise.reject(false);
		}
	}
}
